package com.golddaymicromall.activity.eleaccounts;

import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.golddaymicromall.activity.R;
import com.golddaymicromall.adapter.DianZiAdapter;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.fragment.JiFenFragment;
import com.golddaymicromall.fragment.JinHuoFragment;
import com.golddaymicromall.fragment.ShouYiFragment;
import com.golddaymicromall.fragment.YuFuKuanFragment;
import com.golddaymicromall.model.WindowModel;
import com.golddaymicromall.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.*;

public class DianZiZhangHuActivity extends BaseActivity implements ViewPager.OnPageChangeListener, OnClickListener {
    private FragmentManager fm;
    private ViewPager mVp;
    private DianZiAdapter adapter;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private TextView mDianZi_ShouYiTv;
    private TextView mDianZi_YuFuKuanTv;
    private TextView mDianZi_JinhuoTv;
    private TextView DianZi_leftLine;
    private TextView DianZi_rightLine;
    private TextView mzhanghuxuanze;
    private LinearLayout mactivity_dianzizhanghu_btn_back;
    private int ZhuangZhangZhuangChuMoneyflag;
    private boolean Flag = false;
    private LinearLayout mdianzizhuanghu_choice;

    private PopupWindow mPopWindow;
    private LinearLayout shouyi_layout;
    private LinearLayout jinhuo_layout;
    private LinearLayout yufukuan_layout;
    private LinearLayout jifen_layout;
    private int FragId;


    private String OrganizationId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //透明状态栏

        setContentView(R.layout.activity_dian_zi_zhang_hu);

        OrganizationId = SPUtils.get(getApplicationContext(), "OrganizationId", "").toString();
        initView();
        initList();
    }


    private void initView() {
        mactivity_dianzizhanghu_btn_back = (LinearLayout) findViewById(R.id.activity_dianzizhanghu_btn_back);
        mactivity_dianzizhanghu_btn_back.setOnClickListener(this);
        mzhanghuxuanze = (TextView) findViewById(R.id.zhanghuxuanze);
        if (OrganizationId.equals("28")) {
            mzhanghuxuanze.setVisibility(View.GONE);
        }
        mVp = (ViewPager) findViewById(R.id.DianZiActivity_VP);
        mVp.setOnPageChangeListener(this);
        mzhanghuxuanze.setOnClickListener(this);
    }

    private void initList() {
        fm = getSupportFragmentManager();
        if (OrganizationId.equals("29")) {
            fragmentList.add(new ShouYiFragment());
            fragmentList.add(new JinHuoFragment());
            fragmentList.add(new YuFuKuanFragment());
            fragmentList.add(new JiFenFragment());
        } else {
            fragmentList.add(new JinHuoFragment());
        }

        adapter = new DianZiAdapter(fm, fragmentList);
        mVp.setAdapter(adapter);
        WindowModel.WIDTH = getWindowManager().getDefaultDisplay().getWidth();

        mVp.setCurrentItem(0);


    }

    class mVPListener implements OnClickListener {
        private int index = 0;

        public mVPListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mVp.setCurrentItem(index);
            popupHighLight(index);
        }
    }

    public void popupHighLight(int i) {
        int highLight = 0xfff962a8;
        int normal = 0xfff14394;
        shouyi_layout.setBackgroundColor(normal);
        jinhuo_layout.setBackgroundColor(normal);
        yufukuan_layout.setBackgroundColor(normal);
        jifen_layout.setBackgroundColor(normal);
        switch (i) {
            case 0:
                shouyi_layout.setBackgroundColor(highLight);
                break;
            case 1:
                jinhuo_layout.setBackgroundColor(highLight);
                break;
            case 2:
                yufukuan_layout.setBackgroundColor(highLight);
                break;
            case 3:
                jifen_layout.setBackgroundColor(highLight);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


    }

    private void showPopupWindow() {
        View contentView = LayoutInflater.from(DianZiZhangHuActivity.this).inflate(R.layout.popuplayout, null);
        mPopWindow = new PopupWindow(contentView);
        int h = this.getWindowManager().getDefaultDisplay().getHeight();
        int w = this.getWindowManager().getDefaultDisplay().getWidth();

        mPopWindow.setWidth(w / 3);
        // 设置SelectPicPopupWindow弹出窗体的宽
        mPopWindow.setWidth(w / 3);
        // 设置SelectPicPopupWindow弹出窗体的高
        mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        mPopWindow.setFocusable(true);
        mPopWindow.setOutsideTouchable(true);
        // 刷新状态
        mPopWindow.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        mPopWindow.setBackgroundDrawable(dw);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        mPopWindow.setAnimationStyle(R.style.AnimationPreview);
        shouyi_layout = (LinearLayout) contentView
                .findViewById(R.id.shouyi_layout);
        jinhuo_layout = (LinearLayout) contentView
                .findViewById(R.id.jinhuo_layout);
        yufukuan_layout = (LinearLayout) contentView
                .findViewById(R.id.yufukuan_layout);
        jifen_layout = (LinearLayout) contentView
                .findViewById(R.id.jifen_layout);
        shouyi_layout.setOnClickListener(new mVPListener(0));
        jinhuo_layout.setOnClickListener(new mVPListener(1));
        yufukuan_layout.setOnClickListener(new mVPListener(2));
        jifen_layout.setOnClickListener(new mVPListener(3));

        mPopWindow.showAsDropDown(mzhanghuxuanze);
        popupHighLight(FragId);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    public void onPageSelected(int position) {
        FragId = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_dianzizhanghu_btn_back:
                finish();
                break;
            case R.id.zhanghuxuanze:
                if (OrganizationId.equals("29")) {
                    showPopupWindow();
                }
                break;
        }
    }
}
